package convertidor;

import java.util.List;

import javax.el.ELContext;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import model.Cliente;
import model.Cuenta;


@FacesConverter("convertidor.CuentasReporte")
public class CuentasConverterReporte implements Converter{

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		

		List<Cuenta> clientes= this.getDAO();
		for(Cuenta c : clientes){
			if (c.getCtaId().compareToIgnoreCase(value)==0)
			{
				return c;
				
			}
		}	
		System.out.println("Reeetorna null");				
		
		return null;	
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {	
		return ((Cuenta)value).getCtaId();
	}
	
	public List<Cuenta> getDAO(){				
		FacesContext ctx = FacesContext.getCurrentInstance();
		ELContext ec = ctx.getELContext();
		Application app = ctx.getApplication();
		return (List) app.evaluateExpressionGet(ctx,"#{reporteBean.cuentas}",List.class);		
	}
}
